package com.example.ktapplication.activity

import android.app.Activity
import android.content.Intent
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.os.Bundle
import android.view.View
import android.view.View.GONE
import android.view.View.OnClickListener
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.lifecycleScope
import com.example.ktapplication.R
import com.example.ktapplication.utils.SpUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class LaunchActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (SpUtils.getInstance().getValue("GRAY",false)){
            grayApp()
        }
        setContentView(R.layout.activity_launch)
        //透明化-修改状态栏背景色
        setStatusBarColor(this,R.color.tr, true)
        var canGuide:Boolean = SpUtils.getInstance().getValue("Guide",true)
        if (canGuide){
            SpUtils.getInstance().setValue("Guide",false)
            val intent =  Intent(this, GuidePageActivity::class.java)
            startActivity(intent)
            finish()
        }else{
            initView()
        }
    }

    fun setStatusBarColor(
        activity: Activity,
        statusBarColor: Int = -1, //状态栏背景色
        isModifyStatusBarColor: Boolean //状态栏文字，是否更改颜色
    ) {
        val window = activity.window
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        if (statusBarColor != -1)
            window.statusBarColor = activity.resources.getColor(statusBarColor)
        //由于状态栏图标及字体默认都是白色的，所以设置沉浸式状态栏为白色时需要修改状态栏字体颜色
        if (isModifyStatusBarColor)
            activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        else
            activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    }

    private fun initView() {
        val txtTime=findViewById<TextView>(R.id.txt_time)
        val coordinator=findViewById<CoordinatorLayout>(R.id.coordinator)
        //透明化-允许控件延伸至导航栏 3
        coordinator.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        txtTime.setOnClickListener(OnClickListener{
            intentMainActivity()
        })
        lifecycleScope.launch(Dispatchers.IO){
            for (i in 5 downTo 0 step 1) { // 从countdownTime到0递减
                delay(1000L) // 等待1秒
                withContext(Dispatchers.Main) { // 切换到主线程更新UI
                    txtTime.text = "跳过 "+i+"S";
                    if (i==0){
                        txtTime.visibility= GONE
                    }
                }

            }
            withContext(Dispatchers.Main) { // 倒计时结束后的UI更新
                intentMainActivity()
            }
        }

    }

    private fun intentMainActivity(){
        val intent =  Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }


    fun grayApp(){
        val paint = Paint()
        val colorMatrix = ColorMatrix()
        colorMatrix.setSaturation(0f)
        paint.colorFilter = ColorMatrixColorFilter(colorMatrix)
        window.decorView.setLayerType(View.LAYER_TYPE_HARDWARE, paint)
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}